package com.ruoyi.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.UserTableDelect;
import com.ruoyi.system.service.IUserTableDelectService;
import com.ruoyi.web.controller.tool.EmailTool;
import com.ruoyi.web.controller.tool.RedisTool;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Myuser;
import com.ruoyi.system.service.IMyuserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;

/**
 * 用户管理Controller
 *
 * @author ruoyi
 * @date 2022-08-19
 */
@Controller
@RequestMapping("/system/myuser")
public class MyuserController extends BaseController {
    private String prefix = "system/myuser";

    @Autowired
    private IMyuserService myuserService;
    @Resource
    EmailTool emailTool;

    @Autowired
    private IUserTableDelectService userTableDelectService;
    @Resource
    RedisTool redisTool;

    @RequiresPermissions("system:myuser:view")
    @GetMapping()
    public String myuser() {
        return prefix + "/myuser";
    }

    public String randonNumber = "";

    /**
     * 查询用户管理列表
     */
    @RequiresPermissions("system:myuser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Myuser myuser) {
        startPage();
        List<Myuser> list = myuserService.selectMyuserList(myuser);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @RequiresPermissions("system:myuser:export")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Myuser myuser) {
        List<Myuser> list = myuserService.selectMyuserList(myuser);
        ExcelUtil<Myuser> util = new ExcelUtil<Myuser>(Myuser.class);
        return util.exportExcel(list, "用户管理数据");
    }

    /**
     * 新增用户管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存用户管理
     */
    @RequiresPermissions("system:myuser:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Myuser myuser) {
        return toAjax(myuserService.insertMyuser(myuser));
    }

    /**
     * 修改用户管理
     */
    @RequiresPermissions("system:myuser:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Myuser myuser = myuserService.selectMyuserById(id);
        mmap.put("myuser", myuser);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户管理
     */
    @RequiresPermissions("system:myuser:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Myuser myuser) {
        return toAjax(myuserService.updateMyuser(myuser));
    }

    /**
     * 删除用户管理
     */
    @RequiresPermissions("system:myuser:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(myuserService.deleteMyuserByIds(ids));
    }

    /**
     * 发送邮箱注册
     *
     * @param EmailAddress
     * @return
     */
    @PostMapping("SendCode")
    @ResponseBody
    public AjaxResult SendCode(@RequestParam("EmailAddress") String EmailAddress) {
        Jedis jedis = redisTool.getJedis();
        if (jedis.exists(EmailAddress)) {//上一条验证码还存在
            return error("请不要频繁发送验证码！");
        } else {

            int Rad = 0;
            Rad = (int) (Math.random() * 1000000);
            String Reddom = String.valueOf(Rad);
            while (Reddom.length() < 6) {
                Reddom = "0" + Reddom;
            }
            System.out.println(Reddom);
            randonNumber = Reddom;
//            SendSMSTool sendSMSTool=new SendSMSTool();
//            sendSMSTool.SendSMS(Reddom);
            emailTool.sendMail(EmailAddress, "验证码", Reddom, null);
            jedis.set(EmailAddress, String.valueOf(Rad));    //存储
            jedis.expire(EmailAddress, 60 * 50);  //设置时长
            return success("发送成功！");
        }
    }


    /**
     * 添加用户
     *
     * @param myuser 用户信息
     * @return Ajax信息
     */
    @PostMapping("/myadd")
    @ResponseBody
    public AjaxResult myuseradd(@RequestParam("RandonNunber") String RandonNunber, Myuser myuser) {
        String QQID = myuser.getQqId();
        Jedis jedis = redisTool.getJedis();
        String keyvalue = jedis.get(QQID);
        if (!RandonNunber.equals(keyvalue)) {
            return error("验证码错误");
        }
        jedis.del(QQID);
        Myuser myuser2 = new Myuser();
        myuser2.setPhoneNumber(myuser.getPhoneNumber());
        List<Myuser> list = myuserService.selectMyuserList(myuser2);
        if (list.size() != 0) {
            myuserService.deleteMyuserById(list.get(0).getId());
        }
        return toAjax(myuserService.insertMyuser(myuser));
    }

    /**
     * 判断用户之前是否注册
     *
     * @param
     * @return 0表示有用户，1表示未注册
     */
    @PostMapping("/mylist")
    @ResponseBody
    public AjaxResult mygetuserList(@RequestParam("qqid") String qqid) {
        Myuser myuser = new Myuser();
        myuser.setQqId(qqid);
        List<Myuser> list = myuserService.selectMyuserList(myuser);
        return toAjax(list.size());
    }

    /**
     * 获取用户列表，方便用户关注
     *
     * @param myuser
     * @return
     */
    @PostMapping("/getAllUserList")
    @ResponseBody
    public AjaxResult mygetAlluserList(Myuser myuser) {
        myuser.setBanStatus("0");
        List<Myuser> list = myuserService.selectMyuserList(myuser);
        List<Myuser> list2 = new ArrayList<>();
        for (Myuser myuseritem : list) {
            Myuser temp = new Myuser();
            temp.setImageUrl(myuseritem.getImageUrl());
            temp.setUserName(myuseritem.getUserName());
            list2.add(temp);
        }
        return success(list2);
    }

    /**
     * 删除用户，并添加用户
     *
     * @param id
     * @return
     */
    @PostMapping("/myremove")
    @ResponseBody
    public AjaxResult myremove(Long id) {
        Myuser myuser = myuserService.selectMyuserById(id);
        UserTableDelect userTableDelect = new UserTableDelect();
        userTableDelect.setImageUrl(myuser.getImageUrl());
        userTableDelect.setUserName(myuser.getUserName());
        userTableDelectService.insertUserTableDelect(userTableDelect);
        return toAjax(myuserService.deleteMyuserById(id));
    }

    /**
     * 用户修改个人信息
     *
     * @param myuser
     * @return
     */
    @PostMapping("/set")
    @ResponseBody
    public AjaxResult myset(Myuser myuser) {
        return toAjax(myuserService.updateMyuser(myuser));
    }

    @PostMapping("/getUserMessage")
    @ResponseBody
    public AjaxResult getUserMessage(
            @RequestParam("phoneNumber") String phoneNumber,
            @RequestParam("password") String password
    ) {
//        Myuser myuser = new Myuser();
//        myuser.setPassword(password);
//        myuser.setPhoneNumber(phoneNumber);
        Myuser myuser = null;
        myuser = myuserService.DoLogin(phoneNumber, password);
        if (null != myuser) {
            return success(myuser);
        } else {
            return error();
        }
//        return toAjax(myuserService.DoLogin(phoneNumber, password));
    }


    @PostMapping("/getUserImage")
    @ResponseBody
    public AjaxResult getImage(@RequestParam("phoneNumbers") String[] phoneNumbers) {
        List<String> imagesUrl = new ArrayList<>();
        Myuser myuser = new Myuser();
        for (String phone : phoneNumbers) {
            myuser.setPhoneNumber(phone);
            List<Myuser> myusers = myuserService.selectMyuserList(myuser);
            imagesUrl.add(myusers.get(0).getImageUrl());
        }
        return success(imagesUrl);
    }
}
